#!/bin/bash

# 此脚本用于修复应用程序目录链接问题
# 需要在恢复模式下执行

echo "正在准备修复应用程序链接..."

# 备份现有应用程序目录
if [ -d "/Applications" ] && [ ! -L "/Applications" ]; then
    echo "备份现有应用程序目录..."
    mv "/Applications" "/Applications.original"
elif [ -L "/Applications" ]; then
    echo "删除现有链接..."
    rm -f "/Applications"
fi

# 创建正确的链接
echo "创建指向Sun卷应用程序目录的链接..."
ln -s "/Volumes/Sun/Applications" "/Applications"

# 确保系统应用可用
if [ -d "/Applications.original" ]; then
    echo "恢复系统应用..."
    for app in "/Applications.original/"*.app; do
        app_name=$(basename "$app")
        if [[ "$app_name" == "Safari.app" || "$app_name" == "Mail.app" || "$app_name" == "Calendar.app" ]]; then
            echo "保留系统应用: $app_name"
            ln -sf "$app" "/Applications/$app_name"
        fi
    done
    
    # 恢复Utilities目录
    if [ -d "/Applications.original/Utilities" ]; then
        echo "恢复Utilities目录..."
        ln -sf "/Applications.original/Utilities" "/Applications/Utilities"
    fi
fi

echo "链接修复完成!" 